﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class CmpUpdateUI : Form
    {
        public CmpUpdateUI()
        {
            InitializeComponent();
            btnUpdateCmp.Enabled = false;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtCmpId.Text.Trim().Length > 0)
            {
                String sql = "SELECT CmpId,CmpName,Owner,EmpNumber,Contacter,ContacterPhone,Contractor,CreateDate,Remark FROM Company WHERE CmpId='" + txtCmpId.Text.Trim() + "';";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    txtCmpId.Enabled = false;
                    btnUpdateCmp.Enabled = true;
                    this.AcceptButton = btnUpdateCmp;
                    txtCmpName.Text = dt.Rows[0]["CmpName"].ToString();
                    txtOwner.Text = dt.Rows[0]["Owner"].ToString();
                    txtCmpNumber.Text = dt.Rows[0]["EmpNumber"].ToString();
                    txtContacter.Text = dt.Rows[0]["Contacter"].ToString();
                    txtContactorPhone.Text = dt.Rows[0]["ContacterPhone"].ToString();
                    txtContractor.Text = dt.Rows[0]["Contractor"].ToString();
                    txtRemark.Text = dt.Rows[0]["Remark"].ToString();

                    dgvCmpInfo.DataSource = dt;
                }
                else 
                {
                    MessageBox.Show("不能找到数据，请检查你的企业ID号");
                }
            }
            else 
            {
                MessageBox.Show("企业ID号不能为空");
            }
        }

        private void btnUpdateCmp_Click(object sender, EventArgs e)
        {
            String EmpId = txtCmpId.Text.Trim().ToString();
            String EmpName = txtCmpName.Text.Trim().ToString();
            String Owner = txtOwner.Text.Trim().ToString();
            String EmpNumber = txtCmpNumber.Text.Trim().ToString();
            String Contactor = txtContacter.Text.Trim().ToString();
            String ContactorPhone = txtContactorPhone.Text.Trim().ToString();
            String Contractor = txtContractor.Text.Trim().ToString();
            String Remark = txtRemark.Text.Trim().ToString();
            String CreateDate = DateTime.Now.ToString();
            if (EmpId.Length > 0 && EmpName.Length > 0 && Owner.Length > 0 && Contactor.Length > 0 && ContactorPhone.Length > 0 && Contractor.Length > 0)
            {
                String sql = "UPDATE Company SET CmpName='" + EmpName + "',Owner='" + Owner + "',EmpNumber='" + EmpNumber + "',Contacter='" + Contactor + "',ContacterPhone='" + ContactorPhone + "',Contacter='" + Contractor + "',Remark='" + Remark + "' WHERE CmpId='" + EmpId + "'";
               int count = DBSession.ExecuteNonQuery(sql, null);
               if (count > 0)
               {
                   DataTable dtResult = DBSession.ExecuteDataTable("SELECT CmpId,CmpName,Owner,EmpNumber,Contacter,ContacterPhone,Contractor,CreateDate,Remark FROM Company WHERE CmpId='" + EmpId + "';", null);
                    dgvCmpInfo.DataSource = dtResult;
               }

               //clear control value.
               txtCmpId.Text = "";
               txtCmpName.Text = "";
               txtOwner.Text = "";
               txtCmpNumber.Text = "";
               txtContacter.Text = "";
               txtContactorPhone.Text = "";
               txtContractor.Text = "";
               txtRemark.Text = "";
               txtCmpId.Enabled = true;
               txtCmpId.Focus();
               btnUpdateCmp.Enabled = false;
               this.AcceptButton = btnSearch;
            }
            else
            {
                MessageBox.Show("除备注外，其余信息不能为空！");
            }
        }
    }
}
